import {Trigger} from "../w3ts/handles/trigger";

/** @noSelf **/
export default class DebugUtil {
    static noDebug = false;

    static showText(text: string) {
        if (DebugUtil.noDebug) {
            return
        }
        DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 30,
            text)
    }

    static countExecuteTime(func: () => void): number {
        // @ts-ignore
        let o: NoSelf = os;
        let start = o.clock();
        func();
        let useTime = o.clock() - start;
        // @ts-ignore
        let stringTs: NoSelf = string
        let info = '函数执行时间=' + stringTs.format("%.3f", (useTime * 1000)) + "毫秒";
        DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 20, info);
        print(info);
        return useTime
    }


    static onTime(time: number, actionFunc: (this: void) => void) {
        let trigger = new Trigger();
        trigger.registerTimerEvent(time, false)
        trigger.addAction(actionFunc)
    }

    static onChat(char: string, actionFunc: (this: void) => void) {
        let trigger = new Trigger();
        trigger.registerAnyPlayerChatEvent(char, true)
        trigger.addAction(actionFunc)
    }


    static limitTimeCache: { [id: string]: number } = {}

    static isLimitTime(key: string, time: number): boolean {
        if (!DebugUtil.limitTimeCache[key]) {
            DebugUtil.limitTimeCache[key] = 1;
        }
        if (DebugUtil.limitTimeCache[key] > time) {
            return true;
        }
        DebugUtil.limitTimeCache[key] = DebugUtil.limitTimeCache[key] + 1;
        return false;
    }

}
